package com.sasu.university_data.controller.after;

import com.sasu.university_data.entity.Score;
import com.sasu.university_data.service.ScoreService;
import com.sasu.university_data.util.ResponseCode;
import com.sasu.university_data.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "分数线功能接口")
@RestController
@RequestMapping("/score")
public class ScoreController {
    @Resource
    private ScoreService scoreService;

    /***
     * 分页查询数据
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询数据")
    @GetMapping("/select")
    public ResponseData selectByPage( Integer sId, Integer page, Integer pageSize){

        List<Score> scores = scoreService.selectByPage(sId, page, pageSize);

        if (scores==null){
            return new ResponseData(ResponseCode.NULL_DATA);
        }

        long count = scoreService.count(sId);

        return new ResponseData(ResponseCode.SUCCESS,scores,count);
    }

    /**
     * 新增数据
     *
     * @param score 实例对象
     * @return 是否成功
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseData add (@RequestBody Score score ) {

        if (!scoreService.insert(score)){
            return new ResponseData(ResponseCode.ADD_FAILED);
        }

        long count = scoreService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 更新数据
     *
     * @param score 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseData edit (@RequestBody Score score ) {

        if (!scoreService.update(score)){
            return new ResponseData(ResponseCode.UPDATE_FAILED);
        }

        long count = scoreService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 通过主键删除数据
     *
     * @param sId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/delete")
    public ResponseData deleteById ( Integer sId ) {

        if (!scoreService.deleteById(sId)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = scoreService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /***
     * 通过id批量删除数据
     * @param sIds
     * @return
     */
    @ApiOperation("通过sIds批量删除数据")
    @PostMapping("/lotDelete")
    public ResponseData deleteByIds(int[] sIds){

        if (!scoreService.deleteByIds(sIds)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = scoreService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
}
